package com.qqbot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 使用fastjson.jar包进行json和对象之间的转换
 *
 */
public class JsonUtil {
    /**
     * 支持转换
     * SerializerFeature.WriteMapNullValue：保持空字符串输出；如：headPath:null
     * SerializerFeature.WriteDateUseDateFormat：保持格式化输出日期格式，如：2021-02-02 11:18:29
     *
     * @param object    待转换的对象
     * @param enumClazz 包含的枚举类
     * @return json字符串
     */
    public static String convertObject(Object object, Class<? extends Enum>... enumClazz) {
        if (null == object) {
            return null;
        }
        if (null != enumClazz && 0 < enumClazz.length) {
            final SerializeConfig config = new SerializeConfig();
            Arrays.asList(enumClazz).forEach(myEnum -> config.configEnumAsJavaBean(myEnum));
            return JSON.toJSONString(object, config, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        } else {
            return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        }
    }

    /**
     * 转换为json字符串
     *
     * @param object
     * @param features
     * @return
     */
    public static String toJson(Object object, SerializerFeature... features) {
        if (null == object) {
            return null;
        }
        if (null == features || features.length <= 0) {
            return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        }
        return JSON.toJSONString(object, features);
    }

    /**
     * 将json字符串转换成Java对象，使用JSONObject
     * JSON字符串--》》java对象
     *
     * @param json
     * @param <T>
     * @return
     */
    public static <T> T jsonToEntity(String json, Class<T> clazz) {
        if (StringUtils.isNotEmpty(json)) {
            T obj = JSONObject.parseObject(json, clazz);
            return obj;
        }
        return null;
    }

    /**
     * 将list对象转换成json字符串
     * list--》》json字符串
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> String listToJson(List<T> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            return JSON.toJSONString(list, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        }
        return null;
    }

    /**
     * 将json字符串转换成list对象
     * json字符串--》》list
     *
     * @param json
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(String json, Class<T> clazz) {
        if (StringUtils.isNotEmpty(json)) {
            return JSONArray.parseArray(json, clazz);
        }
        return null;
    }

    /**
     * 判断字符串是否是JSON格式
     *
     * @param content
     * @return
     */
    public static JSONObject isJson(String content) {
        try {
            return JSONObject.parseObject(content);
        } catch (Exception e) {
            return null;
        }
    }

}
